﻿#region License Information
// Copyright (c) 2005 - 2009, Allan Spartacus Mangune (allan@owlpal.com)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list 
//   of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice, this list of 
//   conditions and the following disclaimer in the documentation and/or other materials provided 
//   with the distribution.
//
// * Neither the name of Owlpal nor the names of its contributors may be used to endorse or 
//   promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace Owlpal.Models
{
    public class Newsletter
    {
        #region Members
        private Guid id;
        private DateTime dateCreated;
        private DateTime dateUpdated;
        private DateTime dateSent;
        private string subject;
        private string body;
        private string status;
        private MembershipUser creator;
        private MembershipUser updator;
        #endregion

        #region Properties
        /// <summary>
        /// 
        /// </summary>
        public Guid ID
        {
            get { return this.id; }
            set { this.id = value; }
        }
        
        /// <summary>
        /// 
        /// </summary>
        public DateTime DateCreated
        {
            get { return this.dateCreated; }
            set { this.dateCreated = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public DateTime DateUpdated
        {
            get { return this.dateUpdated; }
            set { this.dateUpdated = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public DateTime DateSent
        {
            get { return this.dateUpdated; }
            set { this.dateUpdated = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Subject
        {
            get { return this.subject; }
            set { this.subject = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Body
        {
            get { return this.body; }
            set { this.body = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Status
        {
            get { return this.status; }
            set { this.status = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public MembershipUser Creator
        {
            get { return this.creator; }
            set { this.creator = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public MembershipUser Updator
        {
            get { return this.updator; }
            set { this.updator = value; }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Creates a new Newsletter.
        /// </summary>
        /// <returns></returns>
        public Newsletter Create()
        {
            this.id = Guid.NewGuid();
            this.dateCreated = DateTime.Now;
            this.dateUpdated = DateTime.MinValue;
            this.creator = Membership.GetUser(HttpContext.Current.User.Identity.Name);
            Repositories.INewsletterRepository repository = new Repositories.NewsletterRepository();
            return repository.CreateNewsletter(this);
        }

        /// <summary>
        /// Updates an existing Newsletter.
        /// </summary>
        /// <returns></returns>
        public Newsletter Update()
        {
            this.dateUpdated = DateTime.Now;
            this.Updator = Membership.GetUser(HttpContext.Current.User.Identity.Name);
            Repositories.INewsletterRepository repository = new Repositories.NewsletterRepository();
            return repository.UpdateNewsletter(this);
        }

        /// <summary>
        /// Deletes an existing Newsletter.
        /// </summary>
        /// <param name="id"></param>
        public void Delete(Guid id)
        {
            this.id = id;
            DeleteThis();
        }

        private void DeleteThis()
        {
            Repositories.INewsletterRepository repository = new Repositories.NewsletterRepository();
            repository.DeleteNewsletter(this);
        }

        /// <summary>
        /// Deletes an existing Newsletter.
        /// </summary>
        public void Delete()
        {
            DeleteThis();
        }

        /// <summary>
        /// Finds a Newsletter.
        /// </summary>
        /// <param name="id">Unique ID of the newsletter.</param>
        /// <returns></returns>
        public Newsletter Find(Guid id)
        {
            Repositories.INewsletterRepository repository = new Repositories.NewsletterRepository();
            Newsletter newsletter = repository.Find(id);
            if (newsletter != null)
            {
                this.id = newsletter.ID;
                this.subject = newsletter.Subject;
                this.status = newsletter.Status;
                this.body = newsletter.Body;
                this.dateCreated = newsletter.DateCreated;
                this.dateUpdated = newsletter.DateUpdated;
                this.creator = newsletter.Creator;
                this.updator = newsletter.Updator;
            }
            return this;
        }

        
        #endregion
    }
}
